package com.xiaoq.sites.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.sites.entity.BannerEntity;
import com.xiaoq.sites.repository.IBannerRepository;

@Repository
public class BannerRepositoryImpl implements IBannerRepository {
	
	@Autowired
	private Datastore datastore;

	private static final Logger LOG = LoggerFactory.getLogger(BannerRepositoryImpl.class);
	
	@Override
	public List<BannerEntity> findBannerList(String channelCode,String cityCode) {
		Query<BannerEntity> findBannerQuery = datastore.find(BannerEntity.class);
		List<BannerEntity> bannerList = new ArrayList<>();
		if("2000".equals(channelCode)){
			findBannerQuery.field("zoneCity").equal(cityCode);
			findBannerQuery = datastore.find(BannerEntity.class);
		}
		if (StringUtils.isNotEmpty(channelCode)) {
			findBannerQuery.field("channelCode").equal(channelCode).order("-createdAt");
			bannerList = findBannerQuery.asList();

		} else {
			LOG.error("channelCode值传递不能为空！");
		}
		List<BannerEntity> bannerResultList = new ArrayList<>();
		if(bannerList!=null&&bannerList.size()!=0){
			if("2000".equals(channelCode)){
				bannerResultList.add(bannerList.get(0));
			}else{
				int num = 0;
				if(bannerList.size()>5){
					num =5;
				}else{
					num = bannerList.size();
				}
				for(int i=0;i<num;i++){
					bannerResultList.add(i,bannerList.get(i));
				}
			}
		}
		return bannerResultList;
	}
}
